package com.yh.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.yh.bean.ChatPost;
import com.yh.bean.ChatReply;
import com.yh.bean.Game;
import com.yh.bean.GameClass;
import com.yh.bean.User;
import com.yh.bean.UserLoveGameChat;
import com.yh.dao.ChatIndex;
import com.yh.service.ChatInf;


@Service
public class ChatImp implements ChatInf{

	@Autowired
	private ChatIndex ci;
	@Override
	public List<GameClass> selectGameClass() {//得到所有的游戏分类
		// TODO Auto-generated method stub
		List<GameClass> gameClassList= ci.getGameClass();
		return gameClassList;
	}
	
	@Override
	public List<Game> selectGameBygcId(int gcId) { //根据游戏分类Id展示该分类下的游戏
		// TODO Auto-generated method stub
		List<Game> gameList= ci.getGameBygcId(gcId);
		return gameList;
	}

	@Override
	public Map selectAllChat(String postTitle,int currentPage) {  //展示所有的帖子 或者根据搜索展示主贴
		// TODO Auto-generated method stub
		ChatPost cp=new ChatPost();		
		cp.setPostTitle(postTitle);		
	     Page page= (Page) PageHelper.startPage(currentPage, 7);		
		List<ChatPost> chatList= ci.getChat(cp);	
		System.out.println(" 得到的游戏Id "+chatList.get(0).getGame().getGameId());
		int pages=page.getPages();
		Map map=new HashMap();
		map.put("chatList", chatList);
		map.put("pages", pages);
		//System.out.println("总页数为："+pages);
		return map;
	}

	@Override
	public Map<String,Object> selectGameChat(int gameid,int currentPage) {   //根据游戏Id展示主贴
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPage, 3);
		List<ChatPost> gameChatList= ci.getGameChat(gameid);
		int pages=page.getPages();
		int curr=page.getPageNum();
		System.out.println("游戏主贴的page对象"+page);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("gameChatList", gameChatList);
		map.put("pages", pages);
		map.put("curr", curr);
		return map;
	}

	@Override
	public String getGameName(int gameid) {   // 根据游戏Id得到游                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
		// TODO Auto-generated method stub
		String gameName =ci.getGameName(gameid);
		return gameName;
	}

	@Override
	public List<ChatPost> selectGameChatBySearch(String gameName) {  //根据搜索游戏名称 展示该游戏下的所有的帖子
		// TODO Auto-generated method stub
		List<ChatPost> gameChatList= ci.getGameChatBySearch(gameName);
		return gameChatList;
	}

	@Override
	public Game searchGameByName(String gameName) {       //根据游戏名称搜索游戏
		// TODO Auto-generated method stub
		Game game= ci.searchGame(gameName);
		return game;
	}

	@Override
	public Map<String,Object> selectChatReply(int postId,int currentPage) {    //搜索主贴展示下的回复
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPage, 2);
		List<ChatReply> chatReplyList= ci.getChatReply(postId);
		int pages=page.getPages();//得到总页数
		int curr=page.getPageNum();//得到当前页
		Map map=new HashMap();
		map.put("chatReplyList", chatReplyList);
		map.put("curr", curr);
		map.put("pages", pages);
		return map;
	}

	@Override
	public void addChatPost(ChatPost cp) {             //新增主贴
		// TODO Auto-generated method stub
		ci.insertChatPost(cp);
	}

	@Override
	public void addChatReply(ChatReply cr) {          //新增回复
		// TODO Auto-generated method stub
		ci.insertChatReply(cr);
	}

	@Override
	public List<Game> selectAllGame() {             //管理员
		// TODO Auto-generated method stub
		List<Game> gameList =ci.getAllGame();
		return gameList;
	}

	@Override
	public Map<String,Object> adminSelectChat(Map<String, Integer> map1,int currentPages) { //管理员查看正常的帖子
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPages, 7);
		List<ChatPost> adminChatList = ci.adminGetAllChat(map1);
		//System.out.println("查到的帖子记录"+adminChatList);
		int pages=page.getPages();
		Map<String,Object> map =new HashMap<String, Object>();
		
		map.put("adminChatList", adminChatList);
		map.put("pages", pages);
		return map;
	}

	@Override
	public void amdinUpdateChatPost(int postId) {
		// TODO Auto-generated method stub
		ci.updateChatPost(postId);
	}

	@Override
	public void adminDeleteChatPost(int postId) {
		// TODO Auto-generated method stub
		ci.deleteChatReply(postId);//先删除主贴下的所有回复
		ci.deleteChatPost(postId);//删除主贴
	}

	@Override
	public Map<String,Object> adminSearchChat(Map<String,String> map,int currentPage) {//管理员通过搜索展示正常状态下的帖子
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPage, 3);
		List<ChatPost> cpList= ci.adminSearchChat(map);
		int pages=page.getPages();
		System.out.println("通过搜索查询到的帖子集合"+cpList);
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("cpList", cpList);
		map1.put("pages", pages);
		return map1;
	}

	@Override
	public Map<String, Object> adminSelectStopChat(Map<String, Integer> map, int currentPage) {//管理员查看封禁的帖子
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPage, 7);
		List<ChatPost> adminChatList = ci.adminGetAllStopChat(map);
		//System.out.println("查到的帖子记录"+adminChatList);
		int pages=page.getPages();
		Map<String,Object> map1 =new HashMap<String, Object>();
		
		map1.put("adminChatList", adminChatList);
		map1.put("pages", pages);
		return map1;
		
	}

	@Override
	public Map<String, Object> adminSearchStopChat(Map<String, String> map, int currentPage) {//管理员通过搜索展示封禁的帖子
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPage, 3);
		List<ChatPost> cpList= ci.adminSearchStopChat(map);
		int pages=page.getPages();
		System.out.println("通过搜索查询到的帖子集合"+cpList);
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("cpList", cpList);
		map1.put("pages", pages);
		return map1;
	}

	@Override
	public void adminUpdateStopChat(int postId) {  //管理员解封帖子
		// TODO Auto-generated method stub
		ci.updateStopChat(postId);
	}

	@Override
	public void userAddGame(int gameId, int uid) {  //用户添加关注的游戏
		// TODO Auto-generated method stub
		Map<String,Integer> map=new HashMap<String, Integer>();
		map.put("gameId", gameId);
		map.put("uid", uid);
		ci.userAddLoveGame(map);
	}

	@Override
	public boolean testLoveOrNo(int gameId, int uid) {//判断是否已经关注该游戏(还没有被关注返回true，已经被关注过返回false)
		// TODO Auto-generated method stub
		Map<String,Integer> map=new HashMap<String, Integer>();
		map.put("gameId", gameId);
		map.put("uid", uid);
		UserLoveGameChat ulgc= ci.testLoveOrNo(map);
	     boolean result=true;
	     if(ulgc!=null){   //返回的对象不为空，表示该游戏已经关注过了
	    	result=false; 
	     }
		return result;
	}

	@Override
	public void userDeleteLoveGame(int gameId, int uid) {
		// TODO Auto-generated method stub
		Map<String,Integer> map=new HashMap<String, Integer>();
		map.put("gameId", gameId);
		map.put("uid", uid);
		ci.userDeleteLoveGame(map);
	}

	@Override
	public List<Game> getLoveGame(int uid) {
		// TODO Auto-generated method stub
		List<Game> loveGameList= ci.getLoveGame(uid);
		return loveGameList;
	}

	@Override
	public User getUser(int uid) {
		// TODO Auto-generated method stub
		User user= ci.getUser(uid);
		return user;
	}

	@Override
	public Map<String,Object> getUserPost(int uid,int currentPage) {//得到用户的发帖
		// TODO Auto-generated method stub
		Page page= PageHelper.startPage(currentPage, 7);
		List<ChatPost> userChatList= ci.getUserPost(uid);
		int pages= page.getPages();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userChatList", userChatList);
		map.put("pages", pages);
		return map;
	}

	@Override
	public Map<String, Object> getUserReply(int uid, int currentPage) {
		// TODO Auto-generated method stub
		Page page=PageHelper.startPage(currentPage, 4);
		List<ChatReply> userReplyList= ci.getUserReply(uid);
		int pages= page.getPages();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("pages", pages);
		map.put("userReplyList", userReplyList);
		return map;
	}

	@Override
	public Map<String, Object> userGetChatPost(int postId, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
